package com.hnkywl.sts.dao.organ;

import com.hnkywl.sts.entity.organ.Khh;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-5-27
 * Time: 下午6:10
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class QykhhDao extends SimpleHibernateTemplate<Khh, Long> {
    //========
    public Page<Khh> findQykhhList(Long qyjgId,String qyjgjc,String qc,String jc, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Khh> page = new Page<Khh>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (qc != null && !qc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" qc like ? ");
            param.add(qc.trim() + "%");
        }
        if (jc != null && !jc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" jc like ? ");
            param.add(jc.trim() + "%");
        }
        if (qyjgjc != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" qyjg.jc like ? ");
            param.add("%" + qyjgjc.trim() + "%");
        }
        if (qyjgId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" qyjg.id = ? ");
            param.add(qyjgId);
        }
        //==============记录数量===============
        hql = "select count(*) from Khh";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Khh ";
        if (condition.length() != 0)
            hql = hql + " where " + condition +  "order by lastUpdated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    //=======

    public Page<Khh> findQyjgKhhList(Long qyjgId, int pageNo, int pageSize) {
        Page<Khh> page = new Page<Khh>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("dateCreated");
         if (qyjgId != null) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.or(Restrictions.eq("qyjg.id", qyjgId),Restrictions.eq("nbgs.id", qyjgId))});
        } else {
            return this.findAll(page);
        }
    }


    public Page<Khh> findQykhhList(String khhmc,Long qyjgId, int pageNo, int pageSize) {
        Page<Khh> page = new Page<Khh>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("dateCreated");
        if (StringUtils.isNotEmpty(khhmc)) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.like("khhmc", khhmc, MatchMode.START)});
        }else if (qyjgId != null) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.eq("qyjg.id", qyjgId)});
        } else {
            return this.findAll(page);
        }
    }

    /**
     * 判断岗位属性是否重复
     * @param id       岗位id
     * @param propertyValue       岗位属性值
     * @param propertyName       岗位属性名
     * @return
     */
    public Boolean checkUnique(Long id, Object propertyValue,String propertyName) {
        String hql;
        if (propertyValue == null)
            return false;
        if(id == null){
            //修改检验
            hql = "from Khh where " + propertyName + " = ? ";
            return this.existsQuery(hql, propertyValue);
        }else{
            //新增检验
            hql = "from Khh where " + propertyName + " = ? and id <> ?";
            return this.existsQuery(hql, propertyValue,id);
        }
    }

    /**
     * 获取同层次的当前最大的排序号
     *
     * @param qyjgId 企业机构的Id
     * @return
     */
    public Long findMaxPxh(Long qyjgId) {
        String hql = "select MAX(pxh) from Khh where qyjg.id = ?";
        return findLong(hql, qyjgId);
    }

    public Long findMaxPxh(){
        String hql ="select Max(pxh) from Khh where qyjg_id is not null";
        return findLong(hql);
    }


}
